package gov.child.dao.impl;

import gov.child.dao.AbstractDao;
import gov.child.dao.CollectionDao;
import gov.child.dao.exception.EntityDeleteException;
import gov.child.dao.exception.EntityQueryException;
import gov.child.dao.exception.EntitySaveException;
import gov.child.dao.exception.EntityUpdateException;
import gov.child.entities.po.ECollection;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * The type Collection dao. --finish
 *
 * @author fzh
 * @since 2017 /9/10
 */
@Repository("collectionDao")
public class CollectionDaoImpl extends AbstractDao implements CollectionDao {

    @Override
    public ECollection findById(Integer id) throws EntityQueryException {
        Session session = currentSession();
        return (ECollection) session.createQuery("from ECollection where id = :id order by createTime").setParameter("id", id).getSingleResult();
    }

    @Override
    public List findByUserId(Integer userId) throws EntityQueryException {
        Session session = currentSession();
        return session.createQuery("from ECollection where userId = :userId order by createTime")
                .setParameter("userId", userId).list();
    }

    @Override
    public void delete(ECollection collection) throws EntityDeleteException {
        currentSession().delete(collection);
    }

    @Override
    public void save(ECollection collection) throws EntitySaveException {
        currentSession().save(collection);
    }

    @Override
    public void update(ECollection collection) throws EntityUpdateException {
        // TODO maybe need optimization
        currentSession().update(collection);
    }
}
